package cn.neusoft.luofa603.yuejian_29.activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.jude.rollviewpager.RollPagerView;
import java.util.ArrayList;
import java.util.List;
import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.adapter.ClubInfoTabAdapter;
import cn.neusoft.luofa603.yuejian_29.adapter.ImageLoopAdapter;
import cn.neusoft.luofa603.yuejian_29.adapter.RollPagerAdapter;
import cn.neusoft.luofa603.yuejian_29.bean.Club;
import cn.neusoft.luofa603.yuejian_29.fragment.ClubInfoFragment;
import cn.neusoft.luofa603.yuejian_29.fragment.ClubVideoFragment;
import cn.neusoft.luofa603.yuejian_29.fragment.CourseFragment;
import cn.neusoft.luofa603.yuejian_29.model.CollectModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static cn.neusoft.luofa603.yuejian_29.R.id.mViewPager;
import static cn.neusoft.luofa603.yuejian_29.R.id.rollCI;

public class ClubInfoActivity extends AppCompatActivity {
    public Club club;
    private View view;
    private int end;
    private int end2;
    private int end3;
    private  RollPagerView  rollCI;
    private String coresult="";
    private TabLayout tabs;
    private String SessionID,club_id,mod;
    private ViewPager viewPager;
    private List<String> mTitle = new ArrayList<String>();
    private List<Fragment> mFragment = new ArrayList<Fragment>();
    private TextView club_name;
    private Button iv_return;
    private ImageButton club_collect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clubinfo);
        initviews();
        innitEvents();
        intData();
        ClubInfoTabAdapter clubInfoTabAdapter = new ClubInfoTabAdapter(getSupportFragmentManager(),mTitle,mFragment);
        viewPager.setAdapter(clubInfoTabAdapter);
        tabs.setupWithViewPager(viewPager);
        rollCI = (RollPagerView) findViewById(R.id.rollCI);
        rollCI.setAdapter(new ImageLoopAdapter(ClubInfoActivity.this,rollCI));



    }

    public void initviews(){

        tabs = (TabLayout) findViewById(R.id.TabInfo_TL);
        viewPager = (ViewPager)findViewById(R.id.TabInfo_VP);
        mTitle.add("部门简介");
        mTitle.add("精品课程");
        mTitle.add("相关视频");
        mFragment.add(new ClubInfoFragment());
        mFragment.add(new CourseFragment());
        mFragment.add(new ClubVideoFragment());

        club_name=(TextView)findViewById(R.id.club_name);
        iv_return=(Button)findViewById(R.id.iv_return);
        club_collect=(ImageButton)findViewById(R.id.club_collect);

    }
    public void innitEvents(){
        iv_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        club_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isExits();

               if( end3 == 0){
                   deleteCollect();
               }else {
                   clubCollect();
               }


            }
        });
    }
    public void intData(){
        getClubInfo();
        getSessionID();
        isExits();
    }
    public void getClubInfo(){
        final Bundle bundle=getIntent().getExtras();
        club = (Club) bundle.getSerializable("club");
        club_id=club.getId();
        club_name.setText(club.getClub_name());
    }
    public void getSessionID(){
        SharedPreferences sp= getSharedPreferences("sessionid", Context.MODE_PRIVATE);
        SessionID=sp.getString("sessionid", null);

    }
    public void clubCollect(){
        CollectModel collectModel = new CollectModel();
        Call<String> call = collectModel.clubcollect("clubfans","1",club_id,SessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String result= response.body();
                end = Integer.parseInt(result);
                if (result==null){
                    Toast.makeText(ClubInfoActivity.this,"联网失败！",Toast.LENGTH_LONG).show();
                }else {
                    if (end<0){

                        Toast.makeText(ClubInfoActivity.this,"关注失败！",Toast.LENGTH_LONG).show();
                        club_collect.setImageResource(R.drawable.fail);
                    }else {

                        Toast.makeText(ClubInfoActivity.this,"关注成功！",Toast.LENGTH_LONG).show();
                        club_collect.setImageResource(R.drawable.success);
                    }

                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

                Toast.makeText(ClubInfoActivity.this,"请联网后从新尝试！",Toast.LENGTH_LONG).show();
            }
        });

    }
    public void deleteCollect(){
        final CollectModel collectModel = new CollectModel();
        Call<String> call = collectModel.deletecollect("clubfans",club_id,SessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String result= response.body();
                end2 = Integer.parseInt(result);
                if(result==null){
                    Toast.makeText(ClubInfoActivity.this,"联网失败！",Toast.LENGTH_LONG).show();
                }else {
                    if (end2<0){
                        club_collect.setImageResource(R.drawable.success);

                    }
                    else {
                        Toast.makeText(ClubInfoActivity.this,"取消关注成功！",Toast.LENGTH_LONG).show();
                        club_collect.setImageResource(R.drawable.fail);

                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

                Toast.makeText(ClubInfoActivity.this,"请联网后从新尝试！",Toast.LENGTH_LONG).show();

            }
        });
    }
    public void isExits(){
        CollectModel collectModel=new CollectModel();
        Call<String> call=collectModel.isExits("clubfans",club_id,"1",SessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String result=response.body();
                end3 = Integer.parseInt(result);
                if(result==null){
                    Toast.makeText(ClubInfoActivity.this,"联网失败！",Toast.LENGTH_LONG).show();
                }else {
                    if (end3==0){
                       // coresult = "yes";
                        club_collect.setImageResource(R.drawable.success);

                    }
                    else {
                      //  coresult = "no";
                        club_collect.setImageResource(R.drawable.fail);

                    }
                }

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }
}
